﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Data;
using System.Text;
using FacebookApp;
using System.IO;

/// <summary>
/// Summary description for FunctionClass
/// </summary>
public class FunctionClass
{
    public static DataTable AddCart(string productid, string quantity, DataTable _data)
    {
        DataTable dtshoppingcart;
        Ebook InfomationProduct;
        if (_data == null)
        {
            dtshoppingcart = new DataTable("Shoppingcart");
            dtshoppingcart.Columns.Add("ProductID", typeof(String));
            dtshoppingcart.Columns.Add("ProductName", typeof(String));
            dtshoppingcart.Columns.Add("Price", typeof(String));
            dtshoppingcart.Columns.Add("Quantity", typeof(String));
            dtshoppingcart.Columns.Add("Total", typeof(String));
            DataRow rows = dtshoppingcart.NewRow();
            InfomationProduct = EbookService.Ebook_GetById(productid);
            rows["ProductID"] = InfomationProduct.Id.ToString();
            rows["ProductName"] = InfomationProduct.Name.ToString();

            if (InfomationProduct.PriceNew.ToString() == "0.0000")
            {
                rows["Price"] = InfomationProduct.Price.ToString();
                rows["Quantity"] = quantity;
                rows["Total"] =
                    (Decimal.Parse(InfomationProduct.Price.ToString()) * Int32.Parse(quantity)).ToString();
            }
            else
            {
                rows["Price"] = InfomationProduct.PriceNew.ToString();
                rows["Quantity"] = quantity;
                rows["Total"] = (Decimal.Parse(InfomationProduct.PriceNew.ToString()) * Int32.Parse(quantity)).ToString();
            }
            dtshoppingcart.Rows.Add(rows);
        }
        else
        {
            int dem = 0;
            int id = 0;
            dtshoppingcart = _data;
            InfomationProduct = EbookService.Ebook_GetById(productid);
            DataRow rows = dtshoppingcart.NewRow();
            for (int i = 0; i < dtshoppingcart.Rows.Count; i++)
            {
                if (dtshoppingcart.Rows[i]["ProductID"].ToString() == productid)
                {
                    dem++;
                    id = i;
                    quantity = (dtshoppingcart.Rows[i]["Quantity"].ToString());
                    break;
                }
            }
            if (dem == 0)
            {
                rows["ProductID"] = InfomationProduct.Id.ToString();
                rows["ProductName"] = InfomationProduct.Name.ToString();

                if (InfomationProduct.PriceNew.ToString() == "0.0000")
                {
                    rows["Price"] = InfomationProduct.Price.ToString();
                    rows["Quantity"] = quantity;
                    rows["Total"] =
                        (Decimal.Parse(InfomationProduct.Price.ToString()) * Int32.Parse(quantity)).ToString();
                }
                else
                {
                    rows["Price"] = InfomationProduct.PriceNew.ToString();
                    rows["Quantity"] = quantity;
                    rows["Total"] = (Decimal.Parse(InfomationProduct.PriceNew.ToString()) * Int32.Parse(quantity)).ToString();
                }
                dtshoppingcart.Rows.Add(rows);
            }
            else
            {
                quantity = (int.Parse(quantity) + 1).ToString();
                rows["ProductID"] = InfomationProduct.Id.ToString();
                rows["ProductName"] = InfomationProduct.Name.ToString();

                if (InfomationProduct.PriceNew.ToString() == "0.0000")
                {
                    rows["Price"] = InfomationProduct.Price.ToString();
                    rows["Quantity"] = quantity;
                    rows["Total"] =
                        (Decimal.Parse(InfomationProduct.Price.ToString()) * Int32.Parse(quantity)).ToString();
                }
                else
                {
                    rows["Price"] = InfomationProduct.PriceNew.ToString();
                    rows["Quantity"] = quantity;
                    rows["Total"] = (Decimal.Parse(InfomationProduct.PriceNew.ToString()) * Int32.Parse(quantity)).ToString();
                }
                dtshoppingcart.Rows.Add(rows);
                dtshoppingcart.Rows.RemoveAt(id);
            }
        }

        return dtshoppingcart;
        //Session["Cart"] = dtshoppingcart;
    }
    
    public static string encryptData(string sinput)
    {
        // chuyển chuỗi ký tự thành mảng số nguyên
        byte[] bytes = Encoding.UTF8.GetBytes(sinput);
        // tạo lớp MD5 của NET
        MD5 md5 = new MD5CryptoServiceProvider();
        // thực thi phần mã hóa => mảng byte đã băm
        byte[] hash = md5.ComputeHash(bytes);
        // chuyển thành chuỗi ký tự
        StringBuilder sb = new StringBuilder();
        foreach (byte b in hash)
        {
            sb.Append(string.Format("{0:X2}", b));
        }

        return sb.ToString();
    }

    public static string Format_Price(string Price)
    {
        Price = Price.Replace(".0000", "");
        Price = Price.Replace(",", "");
        string tmp = "";
        while (Price.Length > 3)
        {
            tmp = "." + Price.Substring(Price.Length - 3) + tmp;
            Price = Price.Substring(0, Price.Length - 3);
        }
        tmp = Price + tmp + " VNĐ";
        return tmp;
    }

    public static string CatChu(string s)
    {
        if (s.Length > 16)
        {
            return s.Substring(0, 15);
        }
        else
        {
            return s;
        }
    }

    public static string RandomString(int size)
    {
        Random rnd = new Random();
        string srds = "";
        string[] str = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
        for (int i = 0; i < size; i++)
        {
            srds = srds + str[rnd.Next(0, 61)];
        }
        return srds;
    }
}